package com.chenxin.quartz.task.mes;

import com.chenxin.common.utils.ConvertUtils;
import com.chenxin.interact.domain.UrlMap;
import com.chenxin.interact.domain.wmsToMes.WmsToMesReturn;
import com.chenxin.interact.domain.mesNotice.ReturnNoticeDTO;
import com.chenxin.interact.handler.mesHandler.MesReturnHandler;
import com.chenxin.interact.service.IUrlMapService;
import com.chenxin.interact.service.IWmsToMesReturnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("returnNoticeTask")
public class ReturnNoticeTask {
    @Autowired
    private IUrlMapService urlMapService;
    @Autowired
    private MesReturnHandler mesReturnHandler;
    @Autowired
    private IWmsToMesReturnService wmsToMesReturnService;

    public void run(String method) {
        UrlMap urlMap = urlMapService.selectUrlMapByUrlNo("MES_POST_URL");
        if (urlMap == null) {
            log.error("接口【MES_POST_URL】未定义!");
            return;
        }
        // 获取待上报的完成单据
        WmsToMesReturn toMesReturn = wmsToMesReturnService.selectWmsToMesReturn();
        if (toMesReturn == null) {
            log.error("无待上报的退料完成单据!");
            return;
        }
        // 转换数据
        ReturnNoticeDTO returnNoticeDTO = ConvertUtils.sourceToTarget(toMesReturn, ReturnNoticeDTO.class);
        try {
            mesReturnHandler.notice(urlMap, returnNoticeDTO, method);
        } catch (Exception e) {
            log.error("MES退料通知异常:{}", e);
        }
    }
}
